* Figure A1 - Attrition
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* evolution of balanced panel
local num 0
tempfile panel 
tempfile tempdata 
save `panel'
foreach i of numlist 2005/2013 {
local num = `num' + 1
keep if mkt_year <= `i'
keep id mkt_year problem
bys id problem: gen balanced`i' = (_N ==`num')
bys id : gen balanced_miss`i'= (_N == `num')
save `tempdata', replace 
use `panel', replace 
merge m:1 id mkt_year using `tempdata' 
drop _merge 
save `panel', replace
}

local num 0
foreach i of numlist 2005/2013{
egen FS_`i'= count(id) if balanced_miss`i'==1 
egen FS_miss_`i' = count(id) if balanced`i'==1
egen To_`i' = count(id) if originaltvillage==1 & balanced`i' == 1
egen Tn_`i' = count(id) if firstt_2007==1 & balanced`i' == 1
egen C_`i' = count(id) if Tvillage==0 & balanced`i' == 1
}

local num 0
foreach i of numlist 2005/2013{
local num = `num' + 1
replace FS_miss_`i' = FS_miss_`i'/`num'
replace FS_`i' = FS_`i'/`num'
replace To_`i' = To_`i'/`num'
replace Tn_`i' = Tn_`i'/`num'
replace C_`i' = C_`i'/`num'  
}

forvalues i = 2005/2013 {
foreach var of varlist To_`i' Tn_`i' C_`i' FS_`i' FS_miss_`i'{
replace `var' = . if mkt_year != `i'
}
}

egen To = rowtotal(To_*)
egen Tn = rowtotal(Tn_*)
egen C = rowtotal(C_*)
egen FS = rowtotal (FS_2*)
egen FS_miss= rowtotal(FS_miss_*)

replace To = . if To == 0
replace Tn = . if Tn == 0
replace C = . if C == 0
replace FS=. if FS==0
replace FS_miss=. if FS_miss==0

lab var To "Treatment Group 1 (excluding missing outcome data)"
lab var Tn "Treatment Group 2 (excluding missing outcome data)"
lab var C "Control Group (excluding missing outcome data)" 
la var FS "Full Balanced Panel (including missing outcome data)"
la var FS_miss "Balanced Panel (excluding missing outcome data)"

lab var mkt_year "Year"

twoway connected FS FS_miss To Tn C mkt_year, sort ytitle("Number of Respondents", size(small)) graphregion(color(white)) plotregion(icolor(white)) msymbol(T D O) ylabel(0(500)1500, labsize(small)) xlabel(2005(2)2013, labsize(small))
graph export "../output/graphs/af1a.emf", replace
